<template>
	<el-dialog
        v-model="visible"
        :title="!dataForm.id ? '新增' : '修改'"
        :width="600"
        :close-on-click-modal="false" draggable>
		<el-form
            ref="dataFormRef"
            :model="dataForm"
            :rules="dataRules"
            label-width="80px"
            @keyup.enter="onSubmit()">
			<el-form-item prop="dictValue" label="字典值">
				<el-input v-model="dataForm.dictValue" placeholder="字典值"></el-input>
			</el-form-item>
			<el-form-item prop="dictLabel" label="字典标签">
				<el-input v-model="dataForm.dictLabel" placeholder="字典标签"></el-input>
			</el-form-item>
			<el-form-item prop="labelClass" label="标签样式">
				<el-select v-model="dataForm.labelClass" style="width: 100%" clearable>
					<el-option
						v-for="item in [
							{ value: 'primary', label: 'primary' },
							{ value: 'success', label: 'success' },
							{ value: 'info', label: 'info' },
							{ value: 'warning', label: 'warning' },
							{ value: 'danger', label: 'danger' }
						]"
						:key="item.value"
						:label="item.label"
						:value="item.value"
					>
						<el-tag v-if="item.value === 'primary'" style="width: 100%">{{ item.label }}</el-tag>
						<el-tag v-else style="width: 100%" :type="item.value">{{ item.label }}</el-tag>
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item prop="sort" label="排序">
				<el-input-number v-model="dataForm.sort" controls-position="right" :min="0" label="排序"></el-input-number>
			</el-form-item>
			<el-form-item prop="remark" label="备注">
				<el-input v-model="dataForm.remark" placeholder="备注"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="onSubmit()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'
import { dictDataStore } from '@/store/dict_data'

const useDictDataStore = dictDataStore()
const emit = defineEmits(['refresh'])// 定义刷新事件

const visible = ref(false) 
const dataFormRef = ref()
const dataForm = reactive({ 
    id: 0, 
    dictValue: '', 
    dictLabel: '', 
    labelClass: '', 
    sort: 0, 
    remark: '', 
    dictTypeId: 0, // 新增的字段，用于传递字典类型ID
})

const dataRules = ref({
	dictLabel: [{ required: true, message: '字典标签不能为空', trigger: 'blur' }],
	dictValue: [{ required: true, message: '字典值不能为空', trigger: 'blur' }]
})

const init = (id) => {
	visible.value = true
	dataForm.id = 0

	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}

	// id 存在则为修改
	if (id) {
		getDictData(id)
	}
}

const getDictData = async(id) => {
    const data = await useDictDataStore.fetchDataOne(id)
    Object.assign(dataForm, data)
}

const onSubmit = () => {
	dataFormRef.value.validate(async(valid) => {
		if (!valid) {
			return false
		} 

        try {
            await useDictDataStore.saveDictData(dataForm) 
            ElMessage.success('操作成功') 
            visible.value = false 
            emit('refresh') 
        } catch (error) {
            ElMessage.error(error.message || '操作失败') 
        }
    })
}

defineExpose({init, dataForm})
</script>

<style lang="scss" scoped>
</style>